<template>
  <el-drawer v-model="drawer" :direction="direction" @close="closeclick">
    <template #title>
      <h4>{{ title }}</h4>
    </template>
    <template #default>
      <el-form
        ref="ruleFormRef"
        :model="tableData"
        status-icon
        :rules="rules"
        label-width="120px"
        class="demo-ruleForm"
      >
        <el-form-item label="产线名称" prop="aisleId">
          <template #default>
            <el-select
              v-model="tableData.aisleId"
              clearable
              placeholder="请选择产线名称"
              @visible-change="obtainAisleId"
            >
              <el-option
                v-for="item in AisleIds"
                :key="item.aisleId"
                :label="item.aisleName"
                :value="item.aisleId"
              />
            </el-select>
          </template>
        </el-form-item>
        <el-form-item label="设备名称" prop="deviceId">
          <template #default>
            <el-select
              v-model="tableData.deviceId"
              clearable
              placeholder="请选择产线名称"
              @visible-change="obtainDeviceId"
            >
              <el-option
                v-for="item in deviceId"
                :key="item.deviceId"
                :label="item.deviceName"
                :value="item.deviceId"
              />
            </el-select>
          </template>
        </el-form-item>
        <el-form-item label="隔间名称" prop="panName">
          <el-input
            v-model="tableData.panName"
            clearable
            placeholder="请输入隔间名称"
          />
        </el-form-item>
        <el-form-item label="隔间编号" prop="panNumber">
          <el-input
            v-model="tableData.panNumber"
            clearable
            placeholder="请输入隔间编号"
          />
        </el-form-item>
        <el-form-item label="油嘴高度" prop="panX">
          <el-input
            v-model="tableData.panX"
            clearable
            placeholder="请输入油嘴高度"
          />
        </el-form-item>
        <el-form-item label="丝钩油嘴高度" prop="panY">
          <el-input
            v-model="tableData.panY"
            clearable
            placeholder="请输入丝钩油嘴高度"
          />
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            :loading="loading"
            @click="submitForm(ruleFormRef, tableData)"
            >提交</el-button
          >
          <el-button @click="resetForm(ruleFormRef)">取消</el-button>
        </el-form-item>
      </el-form>
    </template>
    <template #footer> </template>
  </el-drawer>
</template>

<script  setup>
import { ref, defineProps, watch, defineEmits, reactive } from 'vue'
import { configuration } from '@/api/equipment'
import { aisleInfo } from '@/api/aisle'
import { panSaveUpdate } from '@/api/pan'
import { ElMessage } from 'element-plus'
import { rulesInteger } from '@/utils/rules'
const props = defineProps({
  title: {
    type: String
  },
  showDrawer: {
    type: Boolean,
    // 必传
    required: true
  },
  amendPan: {
    type: Object
  }
})
// 定义一个变量 接收父组件传递来的值
const tableData = reactive({})
// 侦听传递过来的数据
watch(
  () => props.amendPan,
  (value) => {
    /**
     * 这里要复用 所以进行判断
     * 判断传递来的值是否是空值
     * 如果是空值则证明是 添加 所以要重置并清空表单
     * 如果不是空值则吧数据渲染上去
     */
    console.log(value)
    if (value.panId === null || value.panId === '') {
      ruleFormRef.value.resetFields()
      Object.keys(tableData).forEach((key) => {
        tableData[key] = ''
      })
    } else {
      /**
       * 调用设备 和产线 信息接口
       * 解决选择器 不展示 名称 而展示 id 的问题+
       */
      // 调用产线接口
      aisleInfo().then((res) => {
        // console.log(res)
        AisleIds.value = res.data.map((item) => {
          const obj = {}
          obj.aisleId = item.aisleId
          obj.aisleName = item.aisleName
          return obj
        })
      })
      //  设备信息接口
      configuration().then((res) => {
        // console.log(res)
        deviceId.value = res.data.map((item) => {
          const obj = {}
          obj.deviceId = item.deviceId
          obj.deviceName = item.deviceName
          return obj
        })
      })
    }
    // 吧获取到的数据 合并到 tableData
    Object.assign(tableData, value)
  },
  {
    immediate: true,
    deep: true
  }
)

// 表单校验
const ruleFormRef = ref()

const rules = reactive({
  aisleId: [{ required: true, message: '产线名称不能为空', trigger: 'blur' }],
  deviceId: [{ required: true, message: '设备名称不能为空', trigger: 'blur' }],
  panName: [{ required: true, message: '隔间名称不能为空', trigger: 'blur' }],
  panNumber: [{ required: true, message: '隔间编号不能为空', trigger: 'blur' }],
  panX: [
    { required: true, message: '油嘴高度不能为空', trigger: 'blur' },
    { validator: rulesInteger, trigger: 'blur' }
  ],
  panY: [
    { required: true, message: '丝钩油嘴高度不能为空', trigger: 'blur' },
    { validator: rulesInteger, trigger: 'blur' }
  ]
})
// 提交
const loading = ref(false)
const submitForm = (formEl, tableData) => {
  console.log(tableData)
  const { aisleName, deviceName, lastUpdateTime, ...obj } = tableData
  // console.log(obj)
  // console.log(tableData)
  formEl.validate((valid) => {
    if (valid) {
      loading.value = true
      setTimeout(() => {
        // 验证通过调用修改接口
        panSaveUpdate(obj)
          .then((res) => {
            ElMessage({
              message: res.message,
              type: 'success'
            })
            emit('clickShow', false, res.code)
          })
          .catch((err) => {
            ElMessage.error(err.message)
            emit('clickShow', false)
          })

        loading.value = false
      }, 500)
    } else {
      ElMessage.error('失败')
      return false
    }
  })
}
// 取消提交
const resetForm = (formEl) => {
  if (!formEl) return
  formEl.resetFields()
  drawer.value = false
  emit('clickShow', false)
}

// 抽屉的隐藏显示
const emit = defineEmits(['clickShow'])
const drawer = ref(false)
watch(
  () => props.showDrawer,
  (value) => {
    drawer.value = value
    console.log(drawer.value)
  }
)

// 抽屉方向  rtl / ltr / ttb / btt
const direction = ref('rtl')
// 监听抽屉关闭
const closeclick = () => {
  emit('clickShow', false)
  Object.keys(tableData).forEach((key) => {
    tableData[key] = ''
  })
}

// 获取设备id
const deviceId = ref([])
const obtainDeviceId = (val) => {
  console.log(val)
  if (val) {
    configuration().then((res) => {
      console.log(res)
      deviceId.value = res.data.map((item) => {
        const obj = {}
        obj.deviceId = item.deviceId
        obj.deviceName = item.deviceName
        return obj
      })
    })
  }
}

// 获取所有产线id
const AisleIds = ref([])
const obtainAisleId = (val) => {
  if (val) {
    aisleInfo().then((res) => {
      console.log(res)
      AisleIds.value = res.data.map((item) => {
        const obj = {}
        obj.aisleId = item.aisleId
        obj.aisleName = item.aisleName
        return obj
      })
    })
  }
}
</script>
<style lang="scss" scoped>
::v-deep .el-input {
  width: 265px;
}
</style>
